//
//  HomePage_collection_View.m
//  Food
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomePage_collection_View.h"
#import "BaseHeader.h"
#import "HomePage_Collection_Cell.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "HomePageModel.h"
@implementation HomePage_collection_View

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self getdata];
    }
    return self;
}

- (void) createView
{
    //创建layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth - 50) / 4.0, 70);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    //初始化CollectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    layout.minimumInteritemSpacing = 0;
    [self addSubview:self.collectionView];
    //添加重用池
    [self.collectionView registerClass:[HomePage_Collection_Cell class] forCellWithReuseIdentifier:@"cell"];
}

- (void) getdata
{
    self.dataArry = [NSMutableArray array];
    NSString *str = @"http://121.41.84.91:80/HandheldKitchen/api/home/tblAssort!getFirstgrade.do";
    [AFNetworkHandler getWithAFNByURL:str completion:^(id result) {
        NSArray *tempArr =result[@"data"];
        self.dataArry = [HomePageModel arrayWithModelByArray:tempArr];
        [self createView];
    }];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomePage_Collection_Cell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HomePageModel *model = self.dataArry[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.png", indexPath.row];
    cell.imageView.image = [UIImage imageNamed:str];
    cell.lable.text = model.name;
    for (NSDictionary *dic in model.detaoledArry) {
        NSLog(@"%@", dic[@"name"]);
    }
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArry.count;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
@end
